package com.panda.group.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.panda.group.dao.GroupMapper;
import com.panda.group.dao.GroupMemberAcceMapper;
import com.panda.group.dao.GroupMemberMapper;
import com.panda.group.entity.Group;
import com.panda.group.entity.GroupMember;
import com.panda.group.entity.GroupMemberAcce;
import com.panda.group.entity.MemberInfo;
import com.panda.group.service.GroupService;
import com.panda.sys.entity.User;
import com.panda.utils.Pager;
 
@Service
public class GroupServiceImp implements GroupService {
	@Resource
	private GroupMapper groupMapper;
	@Resource
	private GroupMemberMapper groupMemberMapper;
	@Resource
	private GroupMemberAcceMapper groupMemberAcceMapper;
	
	@Override
	public Pager<Group> findListLimit(Long offSet, Long pageSize, Long uid,String name) {
		List<Group> topics=groupMapper.findListLimit(offSet, pageSize,uid,name);
		Long count=groupMapper.findListCount(uid,name);
		Pager<Group> pager=new Pager<Group>();
		pager.setItems(topics);
		pager.setRowsCount(count);
		return pager;
	}

	@Override
	public List<User> findAllStudent() {
		return groupMapper.findAllStudent();
	}

	@Override
	public void saveOrUpdate(Group group, List<GroupMember>  newmembers) {
		if(group.getId()!=null){
			Group old = groupMapper.selectByPrimaryKey(group.getId());
			old.setName(group.getName());
			old.setTask(group.getTask());
			group.setId(old.getId());
			groupMapper.updateByPrimaryKey(old);
		}else{
			groupMapper.insert(group);
		}
		List<GroupMember> members=groupMemberMapper.findAllMember(group.getId());
		for (GroupMember m : members) {
			groupMemberMapper.deleteByPrimaryKey(m.getId());
		}
		
		for (GroupMember m:newmembers) {
			m.setGid(group.getId());
			groupMemberMapper.insert(m);
		}
	}

	@Override
	public void delete(Long id) {
		groupMapper.deleteByPrimaryKey(id);
		groupMemberMapper.deleteByGid(id);
	}

	@Override
	public List<GroupMember> showMember(Long id) {
		return  groupMemberMapper.findAllMember(id);
	}

	@Override
	public Group findGroupById(Long id) {
		return groupMapper.selectByPrimaryKey(id);
	}

	@Override
	public Pager<MemberInfo> findMemberInfoLimit(Long offSet, Long pageSize, String name,String haveSubmit,Long belong) {
		List<MemberInfo> memberInfos=groupMapper.findMemberInfoLimit(offSet, pageSize,name,haveSubmit,belong);
		Long count=groupMapper.findMemberInfoCount(name,belong);
		Pager<MemberInfo> pager=new Pager<MemberInfo>();
		pager.setItems(memberInfos);
		pager.setRowsCount(count);
		return pager;
	}

	@Override
	public void addScore(Long id, Double score) {
		GroupMember groupMember = groupMemberMapper.selectByPrimaryKey(id);
		groupMember.setScore(score);
		groupMember.setFinish(1);
		groupMemberMapper.updateByPrimaryKey(groupMember);
	}

	@Override
	public List<GroupMemberAcce> findMemberAcce(Long uid, Long gid) {
		return groupMemberAcceMapper.findMemberAcce(uid,gid);
	}

	@Override
	public void saveAcce(GroupMemberAcce acce) {
		groupMemberAcceMapper.insert(acce);
	}

	@Override
	public void deleteAcce(Long id) {
		groupMemberAcceMapper.deleteByPrimaryKey(id);
	}
	 
}
